package com.white.commonlib.engine.TextImgMixer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;

/**
 * Created by Administrator on 2017/5/4 0004.
 */

public class UrlDrawable extends BitmapDrawable
{
    private Bitmap weakBmp;
    private Drawable weakDef;

    public UrlDrawable(Context ctx)
    {
        weakDef = new ColorDrawable(Color.WHITE);
//        weakDef = ctx.getResources().getDrawable(R.drawable.place_holder);
    }

    public void setBmp(Bitmap bmps)
    {
        weakBmp = bmps;
    }

    @Override
    public void draw(Canvas canvas)
    {
        if (weakBmp != null)
        {
            canvas.drawBitmap(weakBmp, 0, 0, getPaint());
        } else
        {
            if (weakDef != null)
            {
                weakDef.setBounds(0, 0, 50, 30);
                weakDef.draw(canvas);
            }
        }
    }

    public void recycle()
    {
        if (weakBmp != null)
        {
            weakBmp.recycle();
            weakBmp = null;
        }

        if (weakDef != null)
        {
            weakDef = null;
        }

    }

    @Override
    public int getIntrinsicWidth()
    {
        if(weakBmp != null)
        {
            return weakBmp.getWidth();
        }else if(weakDef != null)
        {
            return 50;
        }
        return super.getIntrinsicWidth();
    }

    @Override
    public int getIntrinsicHeight()
    {
        if(weakBmp != null)
        {
            return weakBmp.getHeight();
        }else if(weakDef != null)
        {
            return 50;
        }
        return super.getIntrinsicHeight();
    }
}
